package gods.bubble.util;

public class HtmlCleaner {
	public static String clean(String html) {
		String eb = html;

		if (eb.contains("&amp;"))
			eb = eb.replaceAll("&amp;", "&");
		if (eb.contains("&lt;"))
			eb = eb.replaceAll("&lt;", "<");
		if (eb.contains("&gt;"))
			eb = eb.replaceAll("&gt;", ">");
		if (eb.contains("&quot;"))
			eb = eb.replaceAll("&quot;", "\"");
		if (eb.contains("&apos;"))
			eb = eb.replaceAll("&apos;", "'");
		if (eb.contains("&ndash;"))
			eb = eb.replaceAll("&ndash;", " - ");
		if (eb.contains("&raquo;"))
			eb = eb.replaceAll("&raquo;", " ");

		// space in html
		if (eb.contains("&#160;"))
			eb = eb.replaceAll("&#160;", " ");
		if (eb.contains("&nbsp;"))
			eb = eb.replaceAll("&nbsp;", " ");
		if (eb.contains("&#65279;"))
			eb = eb.replaceAll("&#65279;", "");

		String ret = extraTrim(eb);

		return ret;
	}

	public static String extraTrim(String html) {
		StringBuilder sb = new StringBuilder();
		String[] lines = html.split("\n");

		for (String line : lines) {
			String trimedline = line.trim();

			if (trimedline != null && !trimedline.isEmpty()) {
				sb.append(trimedline);
				sb.append("\n");
			}
		}

		return sb.toString();
	}
}
